package org.jeecg.modules.categories.controller;

import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.categories.entity.BlogCategories;
import org.jeecg.modules.categories.service.BlogCategoriesService;
import org.jeecg.modules.articles.service.BlogArticlesService;
import org.jeecg.config.shiro.IgnoreAuth;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.apache.shiro.authz.annotation.RequiresPermissions;

 /**
 * @Description: blog_categories
 * @Author: jeecg-boot
 * @Date:   2025-06-30
 * @Version: V1.0
 */
@Tag(name="blog_categories")
@RestController
@RequestMapping("/blog/categories")
@Slf4j
public class BlogCategoriesController extends JeecgController<BlogCategories, BlogCategoriesService> {
	@Autowired
	private BlogCategoriesService blogCategoriesService;
	
	@Autowired
	private BlogArticlesService blogArticlesService;
	
	/**
	 * 分页列表查询
	 *
	 * @param blogCategories
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "blog_categories-分页列表查询")
	@Operation(summary="blog_categories-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<BlogCategories>> queryPageList(BlogCategories blogCategories,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
        QueryWrapper<BlogCategories> queryWrapper = QueryGenerator.initQueryWrapper(blogCategories, req.getParameterMap());
		Page<BlogCategories> page = new Page<BlogCategories>(pageNo, pageSize);
		IPage<BlogCategories> pageList = blogCategoriesService.page(page, queryWrapper);
		return Result.OK(pageList);
	}
	
	/**
	 * 公开的分类列表查询（无需权限）
	 *
	 * @return
	 */
	@Operation(summary="公开的分类列表查询")
	@GetMapping(value = "/public/list")
	@IgnoreAuth
	public Result<List<BlogCategories>> getPublicCategories() {
		QueryWrapper<BlogCategories> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("status", 1) // 只查询启用的分类
				   .orderByAsc("sort_order");
		List<BlogCategories> categories = blogCategoriesService.list(queryWrapper);
		return Result.OK(categories);
	}
	
	/**
	 *   添加
	 *
	 * @param blogCategories
	 * @return
	 */
	@AutoLog(value = "blog_categories-添加")
	@Operation(summary="blog_categories-添加")
	@RequiresPermissions("categories:blog_categories:add")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody BlogCategories blogCategories) {
		blogCategoriesService.save(blogCategories);
		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param blogCategories
	 * @return
	 */
	@AutoLog(value = "blog_categories-编辑")
	@Operation(summary="blog_categories-编辑")
	@RequiresPermissions("categories:blog_categories:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody BlogCategories blogCategories) {
		blogCategoriesService.updateById(blogCategories);
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "blog_categories-通过id删除")
	@Operation(summary="blog_categories-通过id删除")
	@RequiresPermissions("categories:blog_categories:delete")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		blogCategoriesService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "blog_categories-批量删除")
	@Operation(summary="blog_categories-批量删除")
	@RequiresPermissions("categories:blog_categories:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.blogCategoriesService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "blog_categories-通过id查询")
	@Operation(summary="blog_categories-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<BlogCategories> queryById(@RequestParam(name="id",required=true) String id) {
		BlogCategories blogCategories = blogCategoriesService.getById(id);
		if(blogCategories==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(blogCategories);
	}

	/**
	 * 重新计算所有分类的文章数量
	 */
	@AutoLog(value = "blog_categories-重新计算所有分类文章数量")
	@Operation(summary="blog_categories-重新计算所有分类文章数量")
	@RequiresPermissions("categories:blog_categories:edit")
	@PostMapping("/recalculateArticleCounts")
	public Result<String> recalculateAllArticleCounts() {
		try {
			blogArticlesService.recalculateAllCategoryArticleCounts();
			return Result.OK("重新计算文章数量成功");
		} catch (Exception e) {
			log.error("重新计算文章数量失败", e);
			return Result.error("重新计算文章数量失败: " + e.getMessage());
		}
	}

    /**
    * 导出excel
    *
    * @param request
    * @param blogCategories
    */
    @RequiresPermissions("categories:blog_categories:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, BlogCategories blogCategories) {
        return super.exportXls(request, blogCategories, BlogCategories.class, "blog_categories");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequiresPermissions("categories:blog_categories:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, BlogCategories.class);
    }

}
